package com.qersoft.controller;

import com.qersoft.entity.PersonalScoreAnalysisEntity;
import com.qersoft.entity.ResponseMessage;
import com.qersoft.service.PersonalScoreAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/score-analysis")
public class PersonalScoreAnalysisController {

    @Autowired
    private PersonalScoreAnalysisService scoreAnalysisService;

    // 创建成绩分析
    @PostMapping("/create")
    public ResponseMessage<String> addScoreAnalysis(@RequestBody PersonalScoreAnalysisEntity scoreAnalysis) {
        boolean result = scoreAnalysisService.createPersonalScoreAnalysis(scoreAnalysis);
        if (result) {
            return ResponseMessage.message("成绩分析创建成功");
        } else {
            return ResponseMessage.message("成绩分析创建失败");
        }
    }

    //修改成绩分析
    @PutMapping("/update")
    public ResponseMessage<String> updateScoreAnalysis(@RequestBody PersonalScoreAnalysisEntity scoreAnalysis) {
        boolean result = scoreAnalysisService.updateScoreAnalysis(scoreAnalysis);
        if (result) {
            return ResponseMessage.message("成绩分析更新成功");
        }else{
            return ResponseMessage.message("成绩分析更新失败");
        }
    }

    // 删除指定的成绩分析
    @DeleteMapping("/delete")
    public ResponseMessage<String> deleteScoreAnalysis(@RequestBody Long studentId,
                                                        @RequestParam Long examId) {
        boolean isDeleted = scoreAnalysisService.deleteScoreAnalysis(studentId, examId);
        if (isDeleted) {
            return ResponseMessage.message("删除成功");
        } else {
            return ResponseMessage.message("删除失败");
        }
    }

    // 获取某个学生的成绩分析
    @GetMapping("/student/{studentId}/exam/{examId}")
    public ResponseMessage<PersonalScoreAnalysisEntity> getScoreAnalysis(@PathVariable Long studentId,
                                                                         @PathVariable Long examId) {
        PersonalScoreAnalysisEntity scoreAnalysis = scoreAnalysisService.getScoreAnalysis(studentId, examId);
        if (scoreAnalysis != null) {
            return ResponseMessage.message(scoreAnalysis);
        } else {
            return ResponseMessage.message(null);
        }
    }

    // 获取班级的成绩分析报告
    @GetMapping("/class/exam/{examId}")
    public ResponseMessage<List<PersonalScoreAnalysisEntity>> getClassScoreAnalysis(@PathVariable Long examId) {
        List<PersonalScoreAnalysisEntity> scoreAnalysisList = scoreAnalysisService.getClassScoreAnalysis(examId);
        if (scoreAnalysisList != null && !scoreAnalysisList.isEmpty()) {
            return ResponseMessage.message(scoreAnalysisList);
        } else {
            return ResponseMessage.message(null);
        }
    }

    // 计算学生的平均分
    @GetMapping("/student/{studentId}/average")
    public ResponseMessage<BigDecimal> calculateAverageScore(@PathVariable Long studentId) {
        return scoreAnalysisService.calculateAverageScore(studentId);
    }

    // 计算学生的总分
    @GetMapping("/student/{studentId}/total")
    public ResponseMessage<BigDecimal> calculateTotalScore(@PathVariable Long studentId) {
        return scoreAnalysisService.calculateTotalScore(studentId);
    }

    // 获取学生的排名
    @GetMapping("/student/{studentId}/rank")
    public ResponseMessage<Integer> getRank(@PathVariable Long studentId) {
        return scoreAnalysisService.getRank(studentId);
    }

    // 获取学生成绩的进步或退步情况
    @GetMapping("/score-change/{studentId}")
    public ResponseMessage<String> getScoreChange(@PathVariable Long studentId,String subject) {
        return scoreAnalysisService.calculateScoreChange(studentId, subject);
    }
}
